package com.aaa.gs.service.impl;

import com.aaa.common.entity.ProductReview;
import com.aaa.common.vo.ProductReviewVO;
import com.aaa.gs.dao.ProductReviewDao;
import com.aaa.gs.service.ProductReviewService;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.aaa.common.util.CustomException;
import com.aaa.common.util.ResultStatus;
import com.aaa.common.vo.Page;


import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (ProductReview)表服务实现类
 *
 * @author makejava
 * @since 2025-09-16 09:36:37
 */
@Service("productReviewService")
@Log4j2
public class ProductReviewServiceImpl implements ProductReviewService {
    @Resource
    private ProductReviewDao productReviewDao;

    /**
     * 统计商品带图片的评价数量
     */
    @Override
    public List<ProductReviewVO> queryImageCount(Integer productId) {
        return productReviewDao.queryImageCount(productId);
    }

    /**
     * 统计行数
     */
    @Override
    public List queryCountAll(Integer productId) {
        List<ProductReviewVO> productReviewVOList = productReviewDao.queryCountAll(productId);
        //评价总数
        int totalCount = productReviewVOList.size();
        //好评
        int goodCount = (int)productReviewVOList.stream().filter(productReviewVO -> productReviewVO.getRating() >= 4).count();
        //中评
        int averageCount = (int)productReviewVOList.stream().filter(productReviewVO -> productReviewVO.getRating() >= 3 && productReviewVO.getRating() < 4).count();
        //差评
        int badCount = (int)productReviewVOList.stream().filter(productReviewVO -> productReviewVO.getRating() < 3).count();
        //计算好评率
        double goodRate = Math.round((totalCount > 0 ? (goodCount * 100.0 / totalCount) : 0.0)*100)/100.0;

        //
        List<Object> reviewCountList = new ArrayList<>();
        reviewCountList.add(totalCount);
        reviewCountList.add(goodCount);
        reviewCountList.add(averageCount);
        reviewCountList.add(badCount);
        reviewCountList.add(goodRate);
        return reviewCountList;
        //return productReviewDao.queryCountAll(productId);
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public ProductReviewVO queryById(Integer id) {
        return this.productReviewDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param page 筛选条件
     * @return 查询结果
     */
    @Override
    public PageInfo queryByPage(Page<ProductReviewVO> page) {
        //分页操作  分页参数一定要进行传递
        if (page == null || page.getPageNo() == null || page.getPageNo() == 0 || page.getPageSize() == null || page.getPageSize() == 0) {
            //进行业务编写时，可以抛出自定义异常
            throw new CustomException(ResultStatus.PARAM_NOT_EMPTY.getReturnCode(),
                    ResultStatus.PARAM_NOT_EMPTY.getReturnMessage());
        }
        PageHelper.startPage(page.getPageNo(), page.getPageSize());
        //获取查询对象
        ProductReviewVO productReview = page.getData();
        return new PageInfo(productReviewDao.queryAllByLimit(productReview));
    }

    /**
     * 新增数据
     *
     * @param productReview 实例对象
     * @return 实例对象
     */
    @Override
    public ProductReview insert(ProductReview productReview) {
        this.productReviewDao.insert(productReview);
        return productReview;
    }

    /**
     * 修改数据     *
     * @param productReview 实例对象
     * @return 实例对象
     */
    @Override
    public ProductReviewVO update(ProductReviewVO productReview) {
        this.productReviewDao.update(productReview);
        return this.queryById(productReview.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.productReviewDao.deleteById(id) > 0;
    }
}
